<?php
/**
 * Created by PhpStorm.
 * User: 冯轲
 * Date: 2017-12-6
 * Time: 17:09
 */
namespace app\admin\controller;

use app\admin\AdminCommon;
use think\Controller;
use think\Db;
use think\Hook;
use think\Request;
use think\Session;
use think\Validate;
use code\Code;

class Nodes extends AdminCommon
{
    public function getindex()
    {
        $roles=new \app\admin\model\Nodes();
        $data=$roles->select();
        $this->assign('data',$data);
        return $this->fetch('nodes/index');
    }
    //添加节点
    public function getAdd()
    {
        return $this->fetch('nodes/create');
    }
    //添加保存节点
    public function postDoadd(Request $request){
        $req=$request->except(['__token__','action']);
        $re=\app\admin\model\Nodes::create($req);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '添加成功',
            ];

        }else{
            $data=[
                'status' => 1,
                'msg' => '添加失败',
            ];
        };
        //必须是json数据的返回
        return json($data);
    }
    //编辑节点
    public function getEdit($id)
    {
        $admin=new \app\admin\model\Nodes();
        $data=$admin->where('id',$id)->select();
        $this->assign('data',$data);
        return $this->fetch('nodes/edit');
    }
    //编辑提交
    public function postdoedit(Request $request,$id){
        $result=$request->except(['__token__','action']);
        $nodes=new \app\admin\model\Nodes();

        $re=$nodes->save($result,['id' => $id]);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '更新成功',
            ];
        }else{
            $data=[
                'status' => 1,
                'msg' => '更新失败',
            ];
        };
        return json($data);
    }
    //节点启用
    public function postStart($id){
        //
        $admin=new \app\admin\model\Nodes();
        $result=$admin->save([
            'status'  => '0',
        ],['id' => $id]);
        if($result){
            $data=
                [
                    'status'=>0,
                    'msg'=>'已启用',
                    'id'=>$id,
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$id,
                ];
        }
        return json($data);
    }
    //节点停用
    public function postStop($id){
        //
        $admin=new \app\admin\model\Nodes();
        $result=$admin->save([
            'status'  => '1',
        ],['id' => $id]);
        if($result){
            $data=
                [
                    'status'=>0,
                    'msg'=>'已停用',
                    'id'=>$id,
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$id,
                ];
        }
        return json($data);
    }
    //删除操作
    public function postDelete($id){
        $re=\app\admin\model\Nodes::destroy($id);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '删除成功',
            ];
        }else{
            $data=[
                'status' => 1,
                'msg' => '删除失败',
            ];
        };
        return json($data);
    }
}